<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>订单查询</title>
<meta name="decorator" content="default" />
<link rel="stylesheet" type="text/css"
	href="${ctxStatic}/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="${ctxStatic}/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css"
	href="${ctxStatic}/easyui/demo/demo.css">
<link rel="stylesheet" type="text/css"
	href="${ctxStatic}/font-awesome-4.7.0/css/font-awesome.min.css">
<script type="text/javascript" src="${ctxStatic}/easyui/jquery.min.js"></script>
<script type="text/javascript"
	src="${ctxStatic}/easyui/jquery.easyui.min.js"></script>
</head>
<body>
	<div>
		<div>
			单号：<input class="easyui-textbox" id="code" data-options="prompt:'单号'" style="width:20%;height:22px">
			物料编号：<input class="easyui-textbox" id="queryGoods"  style="width:20%;height:22px">
			物料名称：<input class="easyui-textbox" id="goodsName"  style="width:20%;height:22px">
			执行人：<input class="easyui-combotree" data-options=""  style="width:200px;" id="executor" />
		</div>
		<br>
	开始：<input class="easyui-datebox" id="startTime" value="" style="width:200px">
	结束：<input class="easyui-datebox" id="endTime" value="" style="width:200px">
	<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="sub();">提交</a>
	</div>
	<br/>
	<div id="real">
		<table id="dg" title="订单" class="easyui-datagrid"
			style="width: 100%; height: auto" data-options="
					rownumbers:true,
					singleSelect:true,
					autoRowHeight:false,
					pagination:true,
					showFooter: true">
			<thead>
				<tr>
					<th data-options="field:'oCode',width:200,align:'center'">单号</th>
					<th data-options="field:'executorCode',width:100,align:'center'">执行人</th>
					<th data-options="field:'execDate',width:200,align:'center'">执行时间</th>
					<th data-options="field:'oType',width:100,align:'center',formatter:function(value,row,index){
							if(value==true){
								return '<i class= &quot;fa fa-sign-in &quot; />入库';
							}else if(value==false){
								return '<i class= &quot;fa fa-sign-out &quot; />出库';
							}
						}">类型</th>
					<th data-options="field:'oStatus',width:100,align:'center',formatter:function(value,row,index){
							if(value=='1'){
								return '<i class= &quot;fa fa-check &quot; style=&quot;color:#1D9D73 &quot; />已审核';
							}else if(value=='0'){
								return '<i class= &quot;fa fa-question &quot; />待审核';
							}else if(value=='2'){
								return '<i class= &quot;fa fa-times &quot;style=&quot;color:#FF0000 &quot;  />拒绝';
							}else if(value=='3'){
								return '<i class= &quot;fa fa-exclamation-circle &quot;style=&quot;color:#8D38C8 &quot;  />未完成';
							}
						}">状态</th>
					<th data-options="field:'details',width:100,align:'center',formatter:function(value,row,index){return '点击获取详情';}" >详情</th>
				</tr>
			</thead>
		</table>
	</div>
	<div id="query" style="display: none">
		<table id="dg2" title="订单" class="easyui-datagrid"
			   style="width: 100%; height: auto;" data-options="
					rownumbers:true,
					singleSelect:true">
			<thead>
			<tr>
				<th data-options="field:'oCode',width:200,align:'center'">单号</th>
				<th data-options="field:'executorCode',width:100,align:'center'">执行人</th>
				<th data-options="field:'execDate',width:200,align:'center'">执行时间</th>
				<th data-options="field:'oType',width:100,align:'center',formatter:function(value,row,index){
							if(value==true){
								return '<i class= &quot;fa fa-sign-in &quot; />入库';
							}else if(value==false){
								return '<i class= &quot;fa fa-sign-out &quot; />出库';
							}
						}">类型</th>
				<th data-options="field:'oStatus',width:100,align:'center',formatter:function(value,row,index){
							if(value=='1'){
								return '<i class= &quot;fa fa-check &quot; style=&quot;color:#1D9D73 &quot; />已审核';
							}else if(value=='0'){
								return '<i class= &quot;fa fa-question &quot; />待审核';
							}else if(value=='2'){
								return '<i class= &quot;fa fa-times &quot;style=&quot;color:#FF0000 &quot;  />拒绝';
							}else if(value=='3'){
								return '<i class= &quot;fa fa-exclamation-circle &quot;style=&quot;color:#8D38C8 &quot;  />未完成';
							}
						}">状态</th>
				<th data-options="field:'details',width:100,align:'center',formatter:function(value,row,index){return '点击获取详情';}" >详情</th>
			</tr>
			</thead>
		</table>
	</div>
	
	<!-- 订单详情 -->
	<div id="dlg" class="easyui-dialog" title="订单" style="width:60%;height:auto;padding:10px"
			data-options="
				iconCls: 'icon-save'
			">
		<div style="margin-bottom:5px">
			<span>编号：</span><input  class='easyui-textbox' id="formcode" style='width:30%;height:22px' readonly="true" />
			<span >申请日期：</span> <input class='easyui-textbox' id="formapprovaldate" style='width:40%;height:22px' readonly="true">
		</div >
		<div style="margin-bottom:5px">
			<span >审核人：</span> <input class='easyui-textbox' id="formverifier" style='width:20%;height:22px' readonly="true">
			<span >执行人：</span> <input class='easyui-textbox' id="formexecutor" style='width:20%;height:22px' readonly="true">
			<span >操作人：</span> <input class='easyui-textbox' id="formoperator" style='width:20%;height:22px' readonly="true">
		</div>
		
		<table id="gstb" title="物品详情"
			style="width: 100%; height: auto" data-options="
					rownumbers:true,
					singleSelect:true,
					autoRowHeight:false,
					pagination:true,
					pageSize:10,showFooter: true">
		<thead>
			<tr>
				<th data-options="field:'gscode',width:100,align:'center'">物品编号</th>
				<th data-options="field:'gsname',width:200,align:'center'">物品名称</th>
				<th data-options="field:'gsamount',width:80,align:'center'">物品数量</th>
				<th data-options="field:'remarks',width:150,align:'center'">备注</th>
			</tr>
		</thead>
	</table>

</div>
	
	

	<script type="text/javascript">
		$(function(){
			$('#dlg').dialog('close');
			//执行人
			var data1='${WorkersData}';
			data1="["+data1+"]";
			data1 = eval("("+data1+")");
			$('#executor').combotree({
				data:data1,
				onSelect : function(node) {  
			        //返回树对象  
			        var tree = $(this).tree;  
			        //选中的节点是否为叶子节点,如果不是叶子节点,清除选中  
			        var isLeaf = tree('isLeaf', node.target);  
			        if (!isLeaf) {  
			            //清除选中  
			            $('#executor').combotree('clear');  
			        }  
			    }  
			});
			
			//假分页
			/* var s=[];
			var data = '${list}';
			data=eval("("+data+")");
			$('#dg').datagrid({
				data : getData(data)
			}).datagrid({loadFilter:pagerFilter}).datagrid({loadFilter:pagerFilter}).datagrid({
				//点击详情触发
				onClickCell: function(index,field,value){//index表示第几行，从0开始，field是单元格，value是单元格的值
					if(field=='details'){
						//显示
						$("#dlg").dialog({
							onOpen:function(){
								$("#formcode").textbox('setValue',data[index].code);//编号
								$("#formapprovaldate").textbox('setValue',data[index].approvalDate);//申请日期
								$("#formverifier").textbox('setValue',data[index].verifier);//审核人
								$("#formexecutor").textbox('setValue',data[index].executor);//执行人
								$("#formoperator").textbox('setValue',data[index].operator);//填表人
								//表格中的物品详情
								$('#gstb').datagrid({
									data : getDialogData(data,index)
								}).datagrid({loadFilter:pagerFilter});
							}
						});
					}
				}
			}); */ 
			
			//真分页
			InitPagenation();
		});
		function InitPagenation(){
			var resultData=[];
			 var url = "${ctx}/erp/goodsType/queryOrdersInfo?pageNumber=1&pageSize=10";
			 $('#dg').datagrid('options').url = url; //设置表格数据的来源URL
			 $('#dg').datagrid('reload'); //重新加载表格
             $('#dg').datagrid({
            	onLoadSuccess : function(data){
            		resultData=data;
            	}
            	 
             });    
	         $('#dg').datagrid({
            	//点击详情触发
				onClickCell: function(index,field,value){//index表示第几行，从0开始，field是单元格，value是单元格的值
					if(field=='details'){
						//显示
						$("#dlg").dialog({
							onOpen:function(){
								console.log(resultData);
								$("#formcode").textbox('setValue',resultData.rows[index].oCode);//编号
								$("#formapprovaldate").textbox('setValue',resultData.rows[index].execDate);//申请日期
								$("#formverifier").textbox('setValue',resultData.rows[index].verifier);//审核人
								$("#formexecutor").textbox('setValue',resultData.rows[index].executorCode);//执行人
								$("#formoperator").textbox('setValue',resultData.rows[index].operator);//填表人
								//表格中的物品详情
								$('#gstb').datagrid({
									data : getDialogData2(resultData.rows,index)
								});
							}
						});
					}
				}
            });
			//设置分页控件 
            var p = $('#dg').datagrid("getPager");
            
            $(p).pagination({
                pageSize: 10, //每页显示的记录条数，默认为10 
                pageList: [10, 20, 30], //可以设置每页记录条数的列表 
                onSelectPage: function (pageNumber, pageSize) {
                	var url = "${ctx}/erp/goodsType/queryOrdersInfo?pageNumber="+pageNumber+"&pageSize="+pageSize;
                    $('#dg').datagrid('options').url = url;
                    $('#dg').datagrid('reload'); //重新加载表格
                }
            });
            
		}
		
		
		function getData(data) {
			
			var datasize="";
			var rows=[];
			console.log("长度");
			console.log(data.length);
			if(0==data.length){
				datasize=5;
			}else{
				datasize=data.length;	
			}
			for(var i=0;i<datasize;i++){
				rows.push({
					oCode :data.length==0?"":data[i].code,
					executorCode : data.length==0?"":data[i].executor,
					execDate : data.length==0?"":data[i].time,
					oType : data.length==0?"3":data[i].type,
					oStatus : data.length==0?"":data[i].status,	
					details : data.length==0?"":"详情",	
					remarks : data.length==0?"":data[i].remarks,
				});
			} 
			console.log(rows);
			return rows;
		}
		
		function getDialogData(data,index){
			var rowindex=index;
			var initData = data;
			var rows = [];
			for(var i=0;i<initData[rowindex].goods.length;i++){
				rows.push({
					gscode : initData[rowindex].goods[i].code,
					gsname : initData[rowindex].goods[i].name,
					gsamount : initData[rowindex].goods[i].amount,
					remarks : initData[rowindex].goods[i].remarks
					/* gsspecification : initData[rowindex].goods[i].specification */
				});
			} 
			return rows;
		}
		
		function getDialogData2(data,index){
			var rowindex=index;
			var initData = data;
			var rows = [];
			for(var i=0;i<initData[rowindex].goods.length;i++){
				rows.push({
					gscode : initData[rowindex].goods[i].oCode,
					gsname : initData[rowindex].goods[i].name,
					gsamount : initData[rowindex].goods[i].amount,
					remarks : initData[rowindex].goods[i].remarks
					/* gsspecification : initData[rowindex].goods[i].specification */
				});
			} 
			return rows;
		}
		function pagerFilter(data) {
			if (typeof data.length == 'number'
					&& typeof data.splice == 'function') { // is array
				data = {
					total : data.length,
					rows : data
				}
			}
			var dg = $(this);
			var opts = dg.datagrid('options');
			var pager = dg.datagrid('getPager');
			pager.pagination({
				onSelectPage : function(pageNum, pageSize) {
					
					opts.pageNumber = pageNum;
					opts.pageSize = pageSize;
					pager.pagination('refresh', {
						pageNumber : pageNum,
						pageSize : pageSize
					});
					dg.datagrid('loadData', data);
				}
			});
			if (!data.originalRows) {
				data.originalRows = (data.rows);
			}
			var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
			var end = start + parseInt(opts.pageSize);
			data.rows = (data.originalRows.slice(start, end));
			return data;
		}
		
		//提交查询
		function sub(){
			
			var code=$("#code").val();
			var executor=$("#executor").combotree('getValue');
			var startTime=$("#startTime").datebox('getValue');
			var endTime=$("#endTime").datebox('getValue');
			if(null!=startTime&&""!=startTime){
				startTime=startTime+" 00:00:00";
			}
			if(null!=endTime&&""!=endTime){
				endTime=endTime+" 00:00:00";
			}
			var queryGoods=$("#queryGoods").textbox('getValue');
			var otherParameter=$("#goodsName").textbox('getValue');
			var _list ={}; //提交总参数
			_list={'code':code,
					'executor':executor,
					'startTime':startTime,
					'endTime':endTime,
					'queryGoods':queryGoods,
					'otherParameter':otherParameter};
			
			//异步获取后台数据
			$.ajax({
			    url: '${ctx}/erp/warehouse/queryRelevantOrders',         
			    data: _list,
			    dataType: "json",
			    type: "POST",          
			    success: function (data) {
                    $('#real').hide();
                    $('#query').show();
			    	$('#dg2').datagrid({
						data : getData(data)
					}).datagrid({
						//点击详情触发
						onClickCell: function(index,field,value){//index表示第几行，从0开始，field是单元格，value是单元格的值
							if(field=='details'){
								//显示
								$("#dlg").dialog({
									onOpen:function(){
										$("#formcode").textbox('setValue',data[index].code);//编号
										$("#formapprovaldate").textbox('setValue',data[index].approvalDate);//申请日期
										$("#formverifier").textbox('setValue',data[index].verifier);//审核人
										$("#formexecutor").textbox('setValue',data[index].executor);//执行人
										$("#formoperator").textbox('setValue',data[index].operator);//填表人
										//表格中的物品详情
										$('#gstb').datagrid({
											data : getDialogData(data,index)
										});
									}
								});
							}
						}
					});
			    	
			    }
			}); 
			
		}
	</script>
</body>
</html>